package com.stox.charting.ui.swing.event.listener;

import java.util.Date;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.stox.charting.ui.swing.view.ChartView;
import com.stox.charting.ui.swing.view.ChartingInternalFrameView;

public class RangeSliderChangeListener implements ChangeListener {

	private final ChartingInternalFrameView view;
	private long lastEventTime = 0;
	
	
	public RangeSliderChangeListener(ChartingInternalFrameView view) {
		this.view = view;
	}
	
	@Override
	public void stateChanged(ChangeEvent e) {
		long now = new Date().getTime();
		if((now - lastEventTime) > 10){
			lastEventTime  = now;
			if(view.getPriceChartView() != null && view.getPriceChartView().getChartCount() > 0){
				for(int i = 0; i < view.getPriceChartView().getChartCount(); i++){
					view.getPriceChartView().getChart(i).getModel().fireChartModelChangedEvent("uiUnits");
				}
			}
			for(ChartView chartView : view.getIndicatorChartViews()){
				if(chartView != null && chartView.getChartCount() > 0){
					for(int i = 0; i < chartView.getChartCount(); i++){
						chartView.getChart(i).getModel().fireChartModelChangedEvent("uiUnits");
					}
				}
			}
			view.repaintChartViews();
		}
		
		
	}

}
